package com.heima.media.task;

import com.heima.media.entity.WmNews;
import com.heima.media.service.IAuditService;
import com.heima.media.service.IWmNewsService;
import org.redisson.api.RBlockingDeque;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;

import java.util.Date;
import java.util.concurrent.TimeUnit;

//@Component
public class WmNewsPublishTask {

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private IWmNewsService wmNewsService;
    @Autowired
    private IAuditService auditService;


    @Async
    public void handlerMsg() throws Exception{
        RBlockingDeque<Object> blockingDeque = redissonClient.getBlockingDeque("article-deque");

        while (true){
            Object poll = blockingDeque.poll(30, TimeUnit.SECONDS);
            if(poll!=null){
                Integer newsId = Integer.parseInt(poll.toString());
                WmNews wmNews = wmNewsService.getById(newsId);
                Long articleId = auditService.saveApArticle(wmNews); //自己把createApArticle改成public并且写到IAuditService接口中
                wmNews.setArticleId(articleId);
                wmNews.setStatus(9);
                wmNewsService.updateById(wmNews);

                System.out.println(new Date()+"消费者接收到的消息"+poll);
            }
        }
    }

}
